package xyz.bali16.module.netdisk.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.exception.BusinessException;
import xyz.bali16.module.netdisk.entity.FileStorage;
import xyz.bali16.module.netdisk.enums.FilePermissionEnum;
import xyz.bali16.module.netdisk.mapper.FileStorageMapper;
import xyz.bali16.module.netdisk.model.filestorage.FileStorageAddParam;
import xyz.bali16.module.netdisk.model.filestorage.FileStoragePageParam;
import xyz.bali16.module.netdisk.model.filestorage.FileStorageUpdateParam;
import xyz.bali16.module.netdisk.service.FileStorageService;


import java.util.List;

@Service("fileStorageService")
public class FileStorageServiceImpl extends ServiceImpl<FileStorageMapper, FileStorage> implements FileStorageService {

    @Override
    public FileStorage getFileStorageByFileUserId(String fileUserId, String fileStorageId) {
        System.out.println("getAvailableStoragesByFileUserId(fileUserId):" + getAvailableStoragesByFileUserId(fileUserId));
        System.out.println(fileStorageId);

        return getAvailableStoragesByFileUserId(fileUserId).stream()
                .filter(f -> f.getId().equals(fileStorageId)).findFirst()
                .orElseThrow(() -> new BusinessException("未找到资源仓库"));
    }
    @Override
    public Integer addFileStorageSize(String fileStorageId, Integer size) {
        return baseMapper.addFileStorageSize(fileStorageId, size);
    }

    @Override
    public Integer removeFileStorageSize(String fileStorageId, Integer size) {
        return baseMapper.removeFileStorageSize(fileStorageId, size);
    }

    @Override
    public Integer updateUserInformation(String fileUserId, FileStorage fileStorage) {
        return baseMapper.updateUserInformation(fileUserId, fileStorage);
    }

    @Override
    public void add(FileStorageAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "FileStorageAddParam 参数不能为为空");
        FileStorage fileStorage = new FileStorage();
        BeanUtils.copyProperties(param, fileStorage);
        System.out.println(fileStorage);
        save(fileStorage);
    }

    @Override
    public void update(FileStorageUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "FileStorageAddParam 参数不能为为空");
        FileStorage fileStorage = new FileStorage();
        BeanUtils.copyProperties(param, fileStorage);
        updateById(fileStorage);
    }

    @Override
    public void delete(String fileStorageId) throws Exception {
        if (fileStorageId == null) {
            return;
        }
        removeById(fileStorageId);
    }

    @Override
    public IPage<FileStorage> getPage(FileStoragePageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getId(),param.getFileUserId());
    }


    @Override
    public List<FileStorage> getFileStoragesByPermission(String fileUserId, Integer permission) {
        // admin用户,直接返回所有的文件仓库
        if(fileUserId.equals("0")){
            return this.list();
        }

        List<FileStorage> availableFileStorages = baseMapper.getFileStorageByPermission(fileUserId,permission);
        return availableFileStorages;
    }

    @Override
    public List<FileStorage> getAvailableStoragesByFileUserId(String fileUserId) {
        // admin用户,直接返回所有的文件仓库
        if(fileUserId.equals("0")){
            return this.list();
        }

        return baseMapper.getAvailableStoragesByFileUserId(fileUserId, FilePermissionEnum.FORBID.getStatus()); //大于0的都认为可用
    }




}
